package com.pansoft.openplanet.activity.child;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.zxing.qrcode.MipcaActivityCapture;
import com.efounder.chat.zxing.qrcode.QRCode;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.db.AccountDao;
import com.pansoft.openplanet.util.TCAccountManager;
import com.utilcode.util.ScreenUtils;
import com.utilcode.util.ToastUtils;

import net.sf.json.JSONObject;

/**
 * 授权签名页面(非创建观察者钱包用户手机的界面)
 */
public class AuthorizationSignEnterFragment extends DialogFragment implements View.OnClickListener {

    public static final String TAG = "SignEnterFragment";

    private ImageView ivClose;
    private ImageView ivCode;

    private TextView completeView;


    public static AuthorizationSignEnterFragment newInstance(String address, String data) {

        Bundle args = new Bundle();
        args.putString("address", address);
        args.putString("data", data);
        AuthorizationSignEnterFragment fragment = new AuthorizationSignEnterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public AuthorizationSignEnterFragment() {
    }


    @Override
    public void onStart() {
        super.onStart();

        int screenHeight = ScreenUtils.getScreenHeight();
        getDialog().getWindow()
                .setLayout(WindowManager.LayoutParams.MATCH_PARENT, screenHeight * 3 / 5);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().setCancelable(true);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.assets_dialog_fragment_authorization_sign_enter, container, false);
        initView(rootView);
        initData();
        return rootView;
    }

    private void initData() {
        String address = getArguments().getString("address");

        String data = getArguments().getString("data");
        JSONObject jsonObject = new JSONObject();
        Account account = new AccountDao(getContext()).getAccount(address);
        if (account == null) {
            ivCode.setImageDrawable(null);
            ToastUtils.showShort(R.string.open_planet_no_wallet_found);
            return;
        }
//        if (account.isMaster()) {
//            ivCode.setImageDrawable(null);
//            ToastUtil.showToast(getContext(), "不能观察主钱包！");
//            return;
//        }
        String publicKey = account.getPublicKey();
        if (TextUtils.isDigitsOnly(publicKey)) {
            publicKey = TCAccountManager.convertTo16Radix(publicKey);
        }
        jsonObject.put("walletAddress", address);
        jsonObject.put("data", data);
        jsonObject.put("publicKey", publicKey);
        Bitmap bm = QRCode.setQRcode(jsonObject.toString(), 800, 800);
        if (bm != null) {
            ivCode.setImageBitmap(bm);
        }


    }

    private void initView(View rootView) {

        ivClose = (ImageView) rootView.findViewById(R.id.iv_close);
        ivCode = (ImageView) rootView.findViewById(R.id.iv_code);
        completeView = rootView.findViewById(R.id.tv_complete);
        completeView.setOnClickListener(this);
        ivClose.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_close) {
            dismiss();
        } else if (id == R.id.tv_complete) {
            dismiss();
            getActivity().finish();

        } else if (id == R.id.iv_back) {

        } else if (id == R.id.button_enter) {
            dismiss();
        } else if (id == R.id.iv_code1) {
            //跳转扫描二维码
            Intent intent = new Intent();
            intent.setClass(getActivity(), MipcaActivityCapture.class);
            intent.putExtra(MipcaActivityCapture.NEED_RESULT, "true");
            startActivityForResult(intent, 1);

        }
    }


}
